package com.xingongchang.baolebei.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.XListView;
import com.insthub.BeeFramework.activity.BaseActivity;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.insthub.BeeFramework.view.ToastView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.umeng.analytics.MobclickAgent;
import com.xingongchang.baolebei.BaolebeiApp;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.adapter.MorePopWindow;
import com.xingongchang.baolebei.adapter.TopicAdapter;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.model.UserInfoModel;
import com.xingongchang.baolebei.protocol.PAGINATED;
import com.xingongchang.baolebei.protocol.SESSION;

public class TanDatilactivity extends BaseActivity implements BusinessResponse,
		XListView.IXListViewListener
{
	private UserInfoModel mUserInfoModel;
	private XListView xlistView;
	private boolean isSetAdapter = true;
	private ImageView more_menu, send_topic;
	private TopicAdapter mTopicAdapter;
	private ImageLoader imageLoader;
	private View headerView;
	private SharedPreferences shared;
	private SharedPreferences.Editor editor;
	private String uid;
	private FrameLayout frameLayout;
	private ImageView more_tips, profile_photo;
	int case_id;
	String case_name, case_img, descname;
	MorePopWindow morePopWindow;
	TextView text, good_title, good_desc;
	NetworkInfo mNetworkInfo;// 判断网络是否连接
	NetworkInfo mobNetInfo;
	NetworkInfo wifiNetInfo;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(this));
		setContentView(R.layout.forum);
		// 判断是否连接网络
		ConnectivityManager mConnectivityManager = (ConnectivityManager) TanDatilactivity.this
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		mobNetInfo = mConnectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		wifiNetInfo = mConnectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
		if (mNetworkInfo == null)
		{
				ToastView toast = new ToastView(this, "网络异常，请检查网络设置");
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
		}
		shared = getSharedPreferences("userInfo", 0);
		editor = shared.edit();
		case_id = getIntent().getIntExtra("topicid", 0);
		case_name = getIntent().getStringExtra("name");
		case_img = getIntent().getStringExtra("Imgs");
		descname = getIntent().getStringExtra("descname");
		if (null == mUserInfoModel)
		{
			mUserInfoModel = new UserInfoModel(this);
			mUserInfoModel.getTopic(case_id);
			mUserInfoModel.getAd();
			SESSION session = SESSION.getInstance();
			mUserInfoModel.getTopicUserInfo(session.uid);
			mUserInfoModel.getLetter();
		}
		text = (TextView) findViewById(R.id.text5);
		text.setText(case_name);
		mUserInfoModel.addResponseListener(this);
		headerView = LayoutInflater.from(this).inflate(R.layout.formlayout,
				null);
		profile_photo = (ImageView) headerView.findViewById(R.id.profile_photo);
		good_title = (TextView) headerView.findViewById(R.id.good_title);
		good_desc = (TextView) headerView.findViewById(R.id.good_desc);
		imageLoader.displayImage(case_img, profile_photo, BaolebeiApp.options);
		good_title.setText(case_name);
		good_desc.setText(descname);
		xlistView = (XListView) findViewById(R.id.topic_list);
		frameLayout = (FrameLayout) findViewById(R.id.frameent01);
		xlistView.setPullLoadEnable(true);
		xlistView.setRefreshTime();
		xlistView.setEmptyView(frameLayout);
		xlistView.addHeaderView(headerView);
		xlistView.setXListViewListener(this, 1);

		// 东西
		more_menu = (ImageView) findViewById(R.id.more_menu);
		send_topic = (ImageView) findViewById(R.id.send_topic);
		more_menu.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				finish();
				overridePendingTransition(R.anim.push_left_in,
						R.anim.push_left_out);
			}
		});
		// 发帖
		send_topic.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				uid = shared.getString("uid", "");
				if (uid.equals(""))
				{
					Intent intent = new Intent(TanDatilactivity.this,
							SigninActivity.class);
					startActivity(intent);
					TanDatilactivity.this.overridePendingTransition(
							R.anim.push_buttom_in, R.anim.push_buttom_out);
				} else
				{
					Intent intent = new Intent(TanDatilactivity.this,
							SendTopicActivity.class);
					intent.putExtra("catId", case_id);
					startActivity(intent);
				}
			}
		});
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException
	{
		if (url.endsWith(ProtocolConst.TOPIC))
		{
			if (mUserInfoModel.responseStatus.succeed == 1)
			{
				xlistView.stopRefresh();
				xlistView.stopLoadMore();
				xlistView.setRefreshTime();
				setContent();
				PAGINATED paginated = PAGINATED.fromJson(jo
						.optJSONObject("paginated"));
				if (0 == paginated.more)
				{
					xlistView.setPullLoadEnable(false);
				} else
				{
					xlistView.setPullLoadEnable(true);
				}
				if (paginated.equals(""))
				{
					frameLayout.setVisibility(View.VISIBLE);
				} else
				{
					frameLayout.setVisibility(View.GONE);
				}
			}
			// xlistView.setOnItemClickListener(new OnItemClickListener() {
			// @Override
			// public void onItemClick(AdapterView<?> arg0, View arg1,
			// int arg2, long arg3) {
			// Intent intent = new
			// Intent(TanDatilactivity.this,TopicDetailActivity.class);
			// intent.putExtra("topicid",
			// mUserInfoModel.topicList.get(arg2).topicid);
			// startActivity(intent);
			// }
			// });
			// xlistView.setOnClickListener(new OnClickListener() {
			// @Override
			// public void onClick(View arg0) {
			//
			// }
			// });
		}
	}

	@Override
	public void onDestroy()
	{
		super.onDestroy();
	}

	@Override
	public void onResume()
	{

		super.onResume();
		uid = shared.getString("uid", "");
		if (!uid.equals(""))
		{
			SESSION session = SESSION.getInstance();
			mUserInfoModel.getTopicUserInfo(session.uid);
			mUserInfoModel.getTopic(case_id);
			mUserInfoModel.getAd();
			mUserInfoModel.getLetter();
		}
		MobclickAgent.onPageStart("Profile");
	}

	@Override
	public void onPause()
	{
		super.onPause();
		MobclickAgent.onPageEnd("Profile"); 
	}

	@Override
	public void onRefresh(int id)
	{
		isSetAdapter = true;
		mUserInfoModel.getTopic(case_id);
		mUserInfoModel.getAd();
	}

	@Override
	public void onLoadMore(int id)
	{
		isSetAdapter = false;
		mUserInfoModel.getTopicMore(case_id);
	}

	public void setContent()
	{

		if (mTopicAdapter == null)
		{

			if (mUserInfoModel.topicList.size() != 0)
			{
				mTopicAdapter = new TopicAdapter(TanDatilactivity.this,
						mUserInfoModel.topicList);
				xlistView.setAdapter(mTopicAdapter);
			}

		} else
		{

			if (isSetAdapter == true)
			{
				xlistView.setAdapter(mTopicAdapter);
			} else
			{
				mTopicAdapter.list = mUserInfoModel.topicList;
				mTopicAdapter.notifyDataSetChanged();
			}
		}

	}

	@Override
	public void onStart()
	{

		super.onStart();
	}

	// 获取屏幕宽度
	public int getDisplayMetricsWidth()
	{
		int i = TanDatilactivity.this.getWindowManager().getDefaultDisplay()
				.getWidth();
		int j = TanDatilactivity.this.getWindowManager().getDefaultDisplay()
				.getHeight();
		return Math.min(i, j);
	}

}
